// pages/mySelfInfomation/mySelfInfomation.js
const app = getApp()
Page({

  /**
   * 页面的初始数据，从信息页传递
   */
  data: {},
  bindPickerChange(e) {
    this.setData({
      [e.currentTarget.dataset.type]: e.detail.value,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 从信息页传递数据
    let preData = JSON.parse(options.data)
    if(!preData.show){
      preData.show = '01'
    }
    this.setData(preData)
  },
  bindchange(e) {
    let show = e.detail.value
    this.setData({
      show: show
    })
  },
  uploadIdCard(e) {
    let _this = this
    wx.chooseImage({
      count: 1,
      success(res) {
        wx.uploadFile({
          url: app.globalData.prefix + "/imageAction/uploadImage.do",
          filePath: res.tempFilePaths[0],
          name: 'file',
          success: (res) => {
            const data = JSON.parse(res.data)
            const status = data.status
            if (status === '00') {
              _this.setData({
                [e.currentTarget.dataset.type]: data.result
              })
            } else {
              wx.$showToast(data.errorMsg)
            }
          }
        })
      }
    })
  },
  uploadAvatar(){
    wx.chooseImage({
      count: 1,
      success:(res) => {
        wx.uploadFile({
          url: app.globalData.prefix + "/imageAction/uploadImage.do",
          filePath: res.tempFilePaths[0],
          name: 'file',
          success: (res) => {
            const data = JSON.parse(res.data)
            const status = data.status
            if (status === '00') {
              let userInfo = this.data.userInfo
              userInfo.avatarUrl = data.result
              app.globalData.userInfo = userInfo
              this.setData({
                userInfo: userInfo
              })
            } else {
              wx.$showToast(data.errorMsg)
            }
          }
        })
      }
    })
  },
  formSubmit(e) {
    let formData = e.detail.value
    wx.request({
      url: app.globalData.prefix + '/memberAction/updateMember.do',
      data: {
        openId: app.globalData.userInfo.openId,
        nickName: formData.nickName,
        avatarUrl: this.data.userInfo.avatarUrl,
        addr: formData.addr,
        job1: formData.job1,
        job2: formData.job2,
        codeIn: formData.codeIn,
        cert1: this.data.cert1,
        cert2: this.data.cert2,
        certs: this.data.certs,
        show: this.data.show
      },
      method: 'POST',
      success: (res) => {
        let status = res.data.status
        if (status === '00') {
          wx.$showToast(res.data.result)
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.$showToast(res.data.errorMsg)
        }
      }
    })
  },
  //图片上传
  upload() {
    wx.chooseImage({
      success: (res) => {
        let certs = this.data.certs
        const tempFilePaths = res.tempFilePaths
        let i = tempFilePaths.length
        this.uploadImg({
          url: app.globalData.prefix + "/imageAction/uploadImage.do",
          path: tempFilePaths
        })
      }
    })
  },
  //多张图片上传
  uploadImg(data) {
    let that = this,
      i = data.i ? data.i : 0, // 上传第几张图片
      success = data.success ? data.success : 0, // 上传成功数
      fail = data.fail ? data.fail : 0, // 上传失败数
      certs = data.certs ? data.certs : this.data.certs // 存储后台返回图片地址
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'file',
      success: (res) => {
        // 图片上传成功，图片上传成功的变量+1
        success++
        const resData = JSON.parse(res.data)
        const status = resData.status
        if (status === '00') {
          certs.push(resData.result)
        }
      },
      fail: (res) => {
        // 图片上传失败，图片上传失败的变量+1
        fail++
      },
      complete: () => {
        i++
        if (i == data.path.length) {
          // 图片上传结束
          that.setData({
            certs: certs
          })
        } else {
          // 还有未上传图片
          data.i = i
          data.success = success
          data.fail = fail
          data.certs = certs
          that.uploadImg(data)
        }
      }
    })
  }
})